package com.ossean.projectmanager.projectsfilter;
/**
 * 将筛选程序写成多线程
 * 基本思路是：程序初始化时，读取未完成筛选的数据的总量SUM，然后设置batchSize = SUM/20;
 * 每20个线程中，每个线程负责处理batchSize条数据。
 */
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ossean.projectmanager.lasttabledao.OpenSourceProjectDao;
import com.ossean.projectmanager.lasttabledao.PointersDao;
import com.ossean.projectmanager.lasttabledao.RelativeMemoToOpenSourceProjectDao;
import com.ossean.projectmanager.model.OpenhubProject;
import com.ossean.projectmanager.model.GithubProject;
import com.ossean.projectmanager.model.OpenSourceProject;
import com.ossean.projectmanager.model.SourceForgeProject;
import com.ossean.projectmanager.parttabledao.PartProjectDao;

@Component("ProjectsFilterThread")
@Scope("prototype")
public class ProjectsFilterThread implements Runnable {
	
	//用于标志是否终止当前进程
	boolean keepRunning = true;	
	
	@Resource
	private OpenSourceProjectDao lastProjectDao;

	@Resource
	private PartProjectDao partProjectDao;

	@Resource
	private RelativeMemoToOpenSourceProjectDao matchResultDao;

	Logger logger = Logger.getLogger(this.getClass());
	private final int batchsize = 5000;
	private int MaxId;//
	private int MinId;
	
	//设置当前线程所处理数据的开始记录的id和结束记录的Id
	public void setBorder(int MinId,int MaxId)
	{
		this.MinId = MinId;
		this.MaxId = MaxId;
	}
	
	@Override
	public void run() {
		filterPrjs();
	}
	
	/**
	 * 对open_source_projects表根据各个社区的特定字段做筛选
	 */
	public void filterPrjs() {
		Thread.currentThread().setName("Thread:" + this.MinId + " to " + this.MaxId + ":");
		int startId = this.MinId;//上次批量筛选操作开始的位置
		int endId = this.MinId;//上次批量筛选操作结束的位置
		logger.info(Thread.currentThread().getName() + " begin Reading projects......");
		while (true) {	
    		endId = startId + batchsize;
			
			if(endId > MaxId)
				endId = MaxId;
				
			List<OpenSourceProject> prjsList = lastProjectDao
					.getPrjsByBorder(startId,endId);
			if(prjsList.size()==0){
				startId = endId + 1;
				continue;
			}
			else{
				//logger.info("Filtering projects......");
				for (OpenSourceProject project : prjsList) {
					//logger.info("project id： " + project.getId());
					String prjUrl = project.getUrl();
					String source = "";
					String url = "";
					if (prjUrl == null || "".equals(prjUrl)) {
						lastProjectDao.updateFiltratedPrj(project.getId(), 0);
						continue;
					}
					if (prjUrl.contains("|,|")) { // 即url中包含多个项目来源
						String firstUrl = StringUtils.splitByWholeSeparator(prjUrl,
								"|,|")[0];// 只对第一个，即去重时保留的最热的项目来源做筛选。
						source = StringUtils.splitByWholeSeparator(firstUrl, "|:|")[0]; // 从url字段中取得第一个来源社区。
						url = StringUtils.splitByWholeSeparator(firstUrl, "|:|")[1]; // 获得第一个url
					} else { // url只有一个项目来源
						source = StringUtils.splitByWholeSeparator(prjUrl, "|:|")[0];
						url = StringUtils.splitByWholeSeparator(prjUrl, "|:|")[1];
					}
					if (source != null && source.length() > 0) {
						source = source.toLowerCase();
					} else {
						continue;
					}
					if (source.equals("openhub")) {
						OpenhubProject openhubProject = partProjectDao
								.getOpenHubPrjByUrl(url); // 根据url从openhub的项目分表获得项目信息
						if (openhubProject != null
								&& openhubProject.getName() != null
								&& !"".equals(openhubProject.getName())
								&& openhubProject.getDescription() != null
								&& !"".equals(openhubProject.getDescription())
								&& openhubProject.getCode_repository() != null
								&& !openhubProject.getCode_repository().contains(
										"Add a code location")) { // openhub的筛选条件为name、description不为空，且该项目有版本库
								lastProjectDao.updateFiltratedPrj(project.getId(),
										1); // 筛选标识从0变为1，表示该项目经过筛选新增的
								matchResultDao.deleteMatchResult(
										getTargetTable(project.getId()),
										project.getId()); // 删除该项目的匹配结果，确保无之前的匹配结果
						} else {
							//lastProjectDao.deleteProject(project.getId());
							matchResultDao.deleteMatchResult(
									getTargetTable(project.getId()),
									project.getId()); // 删除该项目的匹配结果
						}
					} else if (source.equals("sourceforge")) {
						SourceForgeProject sourceforgeProject = partProjectDao
								.getSourceForgePrjByUrl(url); // 根据url从SourceForge的项目分表获得项目信息
						if (sourceforgeProject != null
								&& sourceforgeProject.getName() != null
								&& !"".equals(sourceforgeProject.getName())
								&& sourceforgeProject.getDescription() != null
								&& !"".equals(sourceforgeProject.getDescription())
								&& ((sourceforgeProject.getDownload_num() > 0) || (sourceforgeProject
										.getStars() > 0))) {
								lastProjectDao.updateFiltratedPrj(project.getId(),
										1); // 筛选标识从0变为1，表示该项目经过筛选新增的
								matchResultDao.deleteMatchResult(
										getTargetTable(project.getId()),
										project.getId()); // 删除该项目的匹配结果，确保无之前的匹配结果
						} else {
							//lastProjectDao.deleteProject(project.getId());
							matchResultDao.deleteMatchResult(
									getTargetTable(project.getId()),
									project.getId()); // 删除该项目的匹配结果
						}
					} else if (source.equals("oschina") || source.equals("apache")) {
						if (project.getFilration() == 0) {
							lastProjectDao.updateFiltratedPrj(project.getId(), 1); // 筛选标识从0变为1，表示该项目经过筛选新增的
							matchResultDao.deleteMatchResult(
									getTargetTable(project.getId()),
									project.getId()); // 删除该项目的匹配结果，确保无之前的匹配结果
						} 
					} else  
						if(source.equals("github")){
						GithubProject githubProject = partProjectDao
								.getGithubPrjByUrl(url); //根据URL从github分表中获取github
						if(githubProject != null
						   && !"".equals(githubProject.getName())
						   &&githubProject.getName() != null
						   &&githubProject.getForks() > 0)
						{
							lastProjectDao.updateFiltratedPrj(project.getId(),1); // 筛选标识从0变为1，表示该项目经过筛选新增的
							matchResultDao.deleteMatchResult(
									getTargetTable(project.getId()),
									project.getId()); // 删除该项目的匹配结果，确保无之前的匹配结果
						}
						else
						{
							matchResultDao.deleteMatchResult(
									getTargetTable(project.getId()),
									project.getId()); // 删除该项目的匹配结果，确保无之前的匹配结果
						}
						
					}else
					{
						logger.info("Unknown source... source = " + source);
					}
				}
			}
			
			if(endId >= MaxId)
				break;
			
			startId = endId + 1;
		}
		
		logger.info( Thread.currentThread().getName() + "所负责的部分筛选完毕！");
	}

	/**
	 * get the match result table's name
	 * 
	 * @param osp_id
	 * @return
	 */
	public static String getTargetTable(int ospId) {
		String targetTableName = "";
		if (ospId >= 770000) {
			targetTableName = "relative_memo_to_open_source_projects_70";
		} else {
			int a = 1 + ospId / 11000;
			targetTableName = "relative_memo_to_open_source_projects_" + a;
		}
		return targetTableName;
	}

}
